package com.deng.controller;

import com.deng.Service.ColumnService;
import com.deng.model.Column;
import com.deng.model.Paper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author 邓小熙
 * @Date 2022/5/13 1:50
 * @Version 1.0
 */
@Controller
public class ColumnController {
    @Autowired
    ColumnService columnService;

//    Logger logger = Logger.getLogger(this.getClass());
    /**
     * 通过文章数量来推荐专题
     * 专题内文章由多到少排序
     * @return
     */
    @RequestMapping(value = {"/main/column/findColumnByCount","/column/findColumnByCount"})
    public @ResponseBody
    Map<String, List<Column>> findColumByCount(){
        List<Column> columnList = columnService.findColumnByPaperCount();
        Map<String,List<Column>> map = Maps.newHashMap();
        map.put("column", columnList);
        return map;
    }
    /**
     * 查询所有主题
     * @param search
     * @param pageNum
     * @return
     */
    @RequestMapping(value = {"/main/column/findAllColumn/{pageNum}","/column/findAllColumn/{pageNum}"})
    public @ResponseBody
    PageInfo findAllColumn(@RequestParam(value = "search", required = false) String search,
                           @PathVariable("pageNum") int pageNum){
        System.out.println(pageNum);
        PageInfo<Column> pageInfo = columnService.findAllColumnSearch(search, pageNum);

        System.out.println(pageInfo);
        return pageInfo;
    }
    @RequestMapping(value = "/column/updateTheme")
    public String updateTheme(){
        return "InnerLayui/caozuo/colMes";
    }

    @RequestMapping(value = "/column/updateColumnName/{id}")
    public @ResponseBody int updateColumnName(@PathVariable(name = "id") int paperId,@RequestParam("theme") String title){
        return columnService.updateColumnByPaperId(paperId,title);
    }

    /**
     * 后台专题管理
     * @param url
     * @return
     */
//  判断用户是否拥有管理员的权限
    @RequiresPermissions("can_manage")
    @RequestMapping(value = "/column/columnPage")
    public ModelAndView columnPage(@RequestParam(value = "url",required = false) String url){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("InnerLayui/admin/columnPage");
        modelAndView.addObject("url",url);
        return modelAndView;
    }

    /**
     * 后台增加专题
     * @param url
     * @return
     */
    @RequestMapping(value = "/column/addColumn")
    public String addColumn(String url){
        return "InnerLayui/admin/caozuo/addColumn";
    }

    /**
     * 显示所有专题
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/column/allColumn/{pageNum}")
    public @ResponseBody Map<String,Object> allColumn(@PathVariable("pageNum") String pageNum){
        Map<String,Object> map = columnService.allColumn(pageNum);
        map.put("pageNum",pageNum);
        return map;
    }
    @RequestMapping(value = "/column/insertInlet" )
    public @ResponseBody int insertInlet(@RequestParam("userName") String name,@RequestParam("btn") String image){
        return columnService.insertInlet(name,image);
    }

    /**
     * 通过ID删除专题
     * @param id
     * @return
     */
    @RequestMapping(value = "/column/deleteColumn/{id}")
    public @ResponseBody int deleteColumn(@PathVariable("id") int id){
        return columnService.deleteColumnById(id);
    }

    /**
     * 编辑专题(先通过ID查找专题)
     * @param id
     * @return
     */
    @RequestMapping(value = "/column/editColumn/{id}")
    public ModelAndView editColumn(@PathVariable("id") int id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("InnerLayui/admin/caozuo/editColumn");
        Column column = columnService.selectColumnById(id);
        modelAndView.addObject("column",column);
        return modelAndView;
    }
    /**
     * 编辑专题
     * @param id
     * @return
     */
    @RequestMapping(value = "/column/updateInlet/{id}")
    public @ResponseBody int updateInlet(@RequestParam("sTitle") String name,@RequestParam("imgMes")String image,@PathVariable("id") String id){
        return columnService.updateInlet(name, image, id);
    }



    /**
     * 显示所有专题
     * @return
     */

    @RequestMapping(value = "/column/findAllColumn")
    public @ResponseBody Map<String,Object> findAllColumn(){
        Set<String> list = columnService.findAllColumn();
        Map<String ,Object> map = Maps.newHashMap();
        map.put("list",list);
        return map;
    }

    /**
     * 显示指定主题下的文章
     * @param pageNum
     * @param id
     * @return
     */
    @RequestMapping(value = "/main/column/paperListWithColumnId/{pageNum}")
    public @ResponseBody PageInfo<Paper> paperListWithColumnId(@PathVariable(value = "pageNum") int pageNum, @RequestParam("id") int id){
        return columnService.findPapersWithColumnId(id,pageNum);
    }

//    @RequestMapping(value = "/column/findAllColumn")
//    public String findAllColumn(HttpServletRequest request){
////        Map<String,Object> map = columnService.findAllColumn();
////        HashMap<String, Object> map = new HashMap<>();
//        List<Column> list = columnService.findAllColumn();
////        map.put("theme",list);
//        request.setAttribute("lists",list);
//        return "InnerLayui/submit";
//    }
}
